<template>
  <div class="image-container">
    <el-image
      :src="props.imageUrl"
      :fit="props.fit"
      :preview-src-list="[props.imageUrl]"
      :initial-index="0"
      class="custom-image"
      :style="imageStyle"
      @error="handleError"
      @load="handleLoad"
    >
      <template #error>
        <div class="image-error">
          <el-icon><Picture /></el-icon>
          <span>加载失败</span>
        </div>
      </template>
      <template #placeholder>
        <div class="image-placeholder">
          <el-icon class="is-loading"><Loading /></el-icon>
        </div>
      </template>
    </el-image>
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { Picture, Loading } from '@element-plus/icons-vue'

interface Props {
  imageUrl: string
  width?: string | number
  height?: string | number
  fit?: 'fill' | 'contain' | 'cover' | 'none' | 'scale-down'
}

const props = withDefaults(defineProps<Props>(), {
  width: '100%',
  height: 'auto',
  fit: 'contain'
})

const imageStyle = computed(() => ({
  width: typeof props.width === 'number' ? `${props.width}px` : props.width,
  height: typeof props.height === 'number' ? `${props.height}px` : props.height
}))

const handleError = (error: Error) => {
  console.error('Image load error:', error)
}

const handleLoad = (e: Event) => {
  console.log('Image loaded successfully')
}
</script>

<style scoped>
.image-container {
  display: flex;
  justify-content: center;
  align-items: center;
}

.custom-image {
  max-width: 100%;
  object-fit: contain;
}

.image-error {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  color: #909399;
  height: 100%;
}

.image-placeholder {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100%;
  background: #f5f7fa;
}

:deep(.el-image__inner) {
  transition: all 0.3s;
}
</style>